package org.openfans.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.openfans.domain.Article;
import org.openfans.domain.Facade;
import org.openfans.domain.Fans;
import org.openfans.domain.FeedItem;
import org.openfans.domain.News;
import org.openfans.domain.OtherArticle;
import org.openfans.domain.TagFeedItem;
import org.openfans.domain.TechArticle;
import org.openfans.util.ThreadFans;
import org.openfans.web.form.SaveFeedItemForm;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class SaveArticleFormController extends SimpleFormController {
	private Facade facade = null;

	public void setFacade(Facade facade) {
		this.facade = facade;
	}

	public SaveArticleFormController() {
		setCommandName("article");
		setCommandClass(Article.class);
	}

	@Override
	protected ModelAndView processFormSubmission(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		String id = request.getParameter("id");
		Article article=facade.getArticle(Integer.parseInt(id));
		String tagStr = request.getParameter("tagStr");
		tagStr = new String(tagStr.getBytes("ISO-8859-1"), "UTF-8");
		Fans fans = facade.getFans(ThreadFans.get().getFansId());

		facade.saveArticle(fans, tagStr, article);
//		return new ModelAndView("redirect:viewArticle.html?id="
//				+ id);
		return null;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		ThreadFans.authorize();

		int id = Integer.parseInt(request.getParameter("id"));
		Article article=facade.getArticle(id);

		return article;
	}
}
